package com.yitao.cms.service.product;

import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.product.ProductLogisticsInfoDto;
import com.yitao.client.model.product.ProductLogisticsInfoModel;
import com.yitao.client.model.product.QProductLogisticsInfoModel;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;

/**
 * @Author: wwl
 * @Date: 2019/7/1 14:18
 * @Description:
 */
@Service
public class ProductLogisticsInfoService extends GenericEntityService<ProductLogisticsInfoModel, ProductLogisticsInfoDto, QProductLogisticsInfoModel> {
    public ProductLogisticsInfoService(EntityManager entityManager) {
        super(entityManager);
    }

    /**
     * 修改产品关联的物流信息
     * @param productLogisticsInfoModel
     * @return
     */
    @Transactional
    public ProductLogisticsInfoModel saveProductLogistics(ProductLogisticsInfoModel productLogisticsInfoModel){
        if (productLogisticsInfoModel!=null && productLogisticsInfoModel.getProductId()!=null){
            this.deleteProductLogistics(productLogisticsInfoModel.getProductId());
            return repository.save(productLogisticsInfoModel);
        }
        return null;
    }

    /**
     * 根据产品id删除关联物流信息
     * @param productId
     * @return
     */
    @Transactional
    public int deleteProductLogistics(Long productId){
        return repository.updateByWhere(Q.productId.eq(productId) ,it ->
                it.set(Q.isDelete,true));

    }

    /**
     * 根据产品id查询产品信息
     * @param productId
     * @return
     */
    public ProductLogisticsInfoModel findProductLogisticsById(Long productId){
        if (productId!=null){
            return queryFactory.select(Q).from(Q).where(Q.productId.eq(productId).and(Q.isDelete.eq(false))).fetchFirst();
        }
        return null;
    }


}
